//
//  NoticeBoardView.m
//  OASystem
//
//  Created by macos on 14-4-15.
//  Copyright (c) 2014年 macos. All rights reserved.
//

#import "NoticeBoardView.h"

@interface NoticeBoardView ()

@end

@implementation NoticeBoardView
@synthesize passListDic;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        if(!passListDic)
            passListDic = [[NSMutableDictionary alloc] init];
    }
    return self;
}

//返回上一级
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initNavi
{
    //初始化UINavigation
    self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabel.text = @"公告栏";
    titleLabel.font = HEIGHT_FONT(18.0f);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    [self.navigationItem.titleView addSubview:titleLabel];
    
    //添加导航又边用户信息
    //初始化UINavigation左按钮
    UIButton *helloButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 24)];
    [helloButton setTitle:[NSString stringWithFormat:@"你好，%@",USER_NICK_NAME] forState:UIControlStateNormal];
    helloButton.titleLabel.font = HEIGHT_FONT(13.0f);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:helloButton];
    
    //初始化UINavigation左按钮
    UIButton *naviButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviButton setTitle:@"首页" forState:UIControlStateNormal];
    naviButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviButton];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //UITableView分割线去掉
    noticeBoardTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self initNavi];
    [self pullRefresh];
    
    //展示数据+可变数组
    showArr = [[NSMutableArray alloc] initWithArray:[passListDic objectForKey:@"data"]];
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [showArr count]>=10?[showArr count]+1:[showArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell* cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"];
    if (cell == nil)
    {
        NSArray* arry = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
        cell = [arry objectAtIndex:0];
    }
    
    //自定义分割线
    UIImageView *line    = [[UIImageView alloc] initWithFrame:CGRectMake(0, 59, 320, 1)];
    line.backgroundColor = NOTICEBOARD_LINE_COLOR;
    [cell.contentView addSubview:line];
    
    if(showArr.count < 10 || (showArr.count>=10 && indexPath.row<showArr.count))
    {
        NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
        for (UILabel *subview in subviews)
        {
            if(subview.tag == 100)
                [subview removeFromSuperview];
        }
        cell.title.hidden = NO;
        cell.author.hidden = NO;
        cell.time.hidden = NO;
        cell.count.hidden = NO;
        cell.img1.hidden = NO;
        cell.img2.hidden = NO;
        cell.img3.hidden = NO;
        
        //添加数据
        NSString *titleStr  = [NSString stringWithFormat:@"%@",[[showArr objectAtIndex:indexPath.row] objectAtIndex:9]];//@"这只是测试使用的东东啊";
        NSString *authorStr = [NSString stringWithFormat:@"%@",[[showArr objectAtIndex:indexPath.row] objectAtIndex:22]];//@"你好，李四";
        NSString *time      = [NSString stringWithFormat:@"%@",[[showArr objectAtIndex:indexPath.row] objectAtIndex:11]];//@"2014/04/15";
        time                = [time     substringToIndex:10];
        NSString *countStr  = [NSString stringWithFormat:@"%@",[[showArr objectAtIndex:indexPath.row] objectAtIndex:20]];//@"1120";
        
        cell.title.text     = [NSString stringWithFormat:@"%@",titleStr];
        cell.author.text    = [NSString stringWithFormat:@"%@",authorStr];
        cell.time.text      = [NSString stringWithFormat:@"%@",time];
        cell.count.text     = [NSString stringWithFormat:@"%@",countStr];
    //    cell.image.image    = [UIImage  imageNamed:@"ggl_new.png"];
    }
    else
    {
        cell.title.hidden = YES;
        cell.author.hidden = YES;
        cell.time.hidden = YES;
        cell.count.hidden = YES;
        cell.img1.hidden = YES;
        cell.img2.hidden = YES;
        cell.img3.hidden = YES;
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 20, 200, 20)];
        label.tag = 100;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"点击加载更多";
        label.font = HEIGHT_FONT(14.0f);
        [cell.contentView addSubview:label];
    }
    cell.contentView.backgroundColor = NOTICEBOARD_BACKCOLOR;
    cell.selectionStyle              = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(showArr.count < 10 || (showArr.count>=10 && indexPath.row<showArr.count))
    {
        //push到展示界面
        DisplayNoticeView *instance = [[DisplayNoticeView alloc] init];
        instance.passInfoArr        = [NSArray arrayWithArray:[showArr objectAtIndex:indexPath.row]];
        [self.navigationController pushViewController:instance animated:YES];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
        back = 0;
        pageIndex++;
        NSString *index = [NSString stringWithFormat:@"%d",pageIndex];
        
        NSLog(@"index---P%@",index);
        [[SendHttpOperation shareSendHttpOperation] GetWebInfoList:@"GetWebInfoList.ashx" Cur:index Rows:@"10"];
        [SendHttpOperation shareSendHttpOperation].getWebInfoListDelegate = self;
    }
}

#pragma mark 重新加载下拉框
- (void)pullRefresh
{
    if(_refreshHeaderView)
    {
        [_refreshHeaderView removeFromSuperview];
    }
    _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, -80, 320, 80)];
    _refreshHeaderView.delegate = self;
    [noticeBoardTable addSubview:_refreshHeaderView];
    [_refreshHeaderView refreshLastUpdatedDate];
}

#pragma mark EGORefreshTableHeaderDelegate(上下下拉操作)
- (void)reloadTableViewDataSource
{
	product_reloading = YES;
	NSLog(@"开始获取更多数据...");
	//打开线程，加载更多数据
    back = 1;
    [[SendHttpOperation shareSendHttpOperation] GetWebInfoList:@"GetWebInfoList.ashx" Cur:@"0" Rows:@"10"];
    [SendHttpOperation shareSendHttpOperation].getWebInfoListDelegate = self;
}

- (void)passGetWebInfoList:(NSDictionary *)loginDic
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
    if([[loginDic allKeys] containsObject:@"result"])
    {
        if(back ==1)
        {
            if(showArr)
                [showArr removeAllObjects];
        }
        [showArr addObjectsFromArray:[[[loginDic objectForKey:@"result"] objectForKey:@"list"] objectForKey:@"data"]];
        [noticeBoardTable reloadData];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"网络获取数据失败，请重新尝试！"}];
    }
    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:0];

}

- (void)doneLoadingTableViewData
{
    NSLog(@"加载更多结束");
    [self pullRefresh];
	product_reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:noticeBoardTable];
}

#pragma mark UIScrollViewDelegate(上拉下拉判断)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_refreshHeaderView egoRefreshScrollViewDidScroll:noticeBoardTable];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:noticeBoardTable];
}


#pragma mark EGORefreshTableHeaderDelegate Methods
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
	[self reloadTableViewDataSource];
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view
{
	return product_reloading;
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view
{
	return [NSDate date];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
